<?php

namespace app\cmd;

use app\model\GroupInfo;
use app\model\JumpStatPv;
use CacheKey;
use core\lib\Redis;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StatGroupPvCmd extends Command
{
    // 定时器：php console.php stat_group_pv today all
    protected function configure()
    {
        $this->setName('stat_group_pv')
            ->addArgument('date', InputArgument::OPTIONAL, '日期')
            ->addArgument('group_no', InputArgument::OPTIONAL, '组号');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $date = trim(strval($input->getArgument('date')));
        empty($date) && $date = date('Y-m-d', strtotime("-1 day"));
        if (!empty($date) && $date === 'today') {
            $date = date('Y-m-d');
        }
        $groupNo = trim(strval($input->getArgument('group_no')));
        empty($groupNo) && $groupNo = 'all';

        $statModel = new JumpStatPv();
        $groupInfoModel = new GroupInfo();
        $redis = new Redis();

        // 找出所有组
        if (!empty($groupNo) && $groupNo !== 'all') {
            $groupNos = $groupInfoModel->listAll(['group_no' => $groupNo], 'group_no');
        } else {
            $groupNos = $groupInfoModel->listAll([], 'group_no');
        }
        if (empty($groupNos)) {
            echo '执行失败，没有找到组号=' . $groupNo . PHP_EOL;
            logger('stat')->error('StatGroupPvCmd:没有找到组号');
            return Command::INVALID;
        }

        foreach ($groupNos as $gno) {
            echo '开始执行group_no=' . $gno . ',date=' . $date . PHP_EOL;
            // 该组号该日期的总数量
            $cacheKey = sprintf(CacheKey::STATDOMAIN, $date, $gno);
            $totalNum = $redis->get($cacheKey);
            if ($totalNum === false) {
                logger('stat')->error('key：' . $cacheKey . '，它没有统计信息1');
                continue;
            }
            // 找出该组号的所有域名
            $cacheKey = sprintf(CacheKey::GROUPNO_DOMAINS, $gno);
            $domains = $redis->smembers($cacheKey);
            if (empty($domains)) {
                logger('stat')->error('key：' . $cacheKey . '，它没有统计信息2');
                continue;
            }

            foreach ($domains as $domain) {
                $cacheKey = sprintf(CacheKey::JUMPINFO, $date, $gno, $domain);
                $num = $redis->get($cacheKey);
                if ($num === false) {
                    continue;
                }
                $statId = $statModel->have(['stat_date' => $date, 'group_no' => $gno, 'domain' => $domain], 'id');
                $rate = $totalNum > 0 ? ($num == $totalNum ? 100 : round($num * 100 / $totalNum, 2)) : 0;
                if ($statId) {
                    $data = ['num_pv' => $num, 'total_pv' => $totalNum, 'rate_pv' => $rate];
                    $statModel->updateRow($statId, $data);
                    logger('stat')->info('更新statId：' . $statId . '，data=' . json_encode($data, JSON_UNESCAPED_UNICODE));
                    if ($num > 100) {
                        echo '  更新domain=' . $domain . 'num_pv=' . $num . ',total_pv=' . $totalNum . ',rate_pv=' . $rate . PHP_EOL;
                    }
                } else {
                    $data = [
                        'stat_date' => $date, 'group_no' => $gno, 'domain' => $domain,
                        'num_pv' => $num, 'total_pv' => $totalNum, 'rate_pv' => $rate
                    ];
                    $statId = $statModel->save($data);
                    logger('stat')->info('新增statId：' . $statId . '，data=' . json_encode($data, JSON_UNESCAPED_UNICODE));
                    echo '  新增omain=' . $domain . 'num_pv=' . $num . ',total_pv=' . $totalNum . ',rate_pv=' . $rate . PHP_EOL;
                }
            }
            echo '结束执行group_no=' . $gno . PHP_EOL;
        }
        echo '结束执行，程序退出';
        return Command::SUCCESS;
    }
}